// 音频资源
import resourecs from './musicResourecs.js'
let musics = resourecs.musicResourecs

let audio,
	timeout
export default {
	state: {
		playStatus: false, // 播放与暂停标识
		currentPlayIndex: 0, // 当前歌曲标识
		durationTime: 100, // 音频总时长
		currentTime: 0, // 音频播放当前时间
		audioList: []
	},
	getters: {
		// 音频名称
		audioName(state) {
			let curIndex = state.currentPlayIndex
			return musics[curIndex].name
		},
		// 音频作者
		singerName(state) {
			let curIndex = state.currentPlayIndex
			return musics[curIndex].singer.name
		},
		// 音频简介
		singerSynopsis(state) {
			let curIndex = state.currentPlayIndex
			return musics[curIndex].singer.synopsis
		},
		// 获取音频背景图
		CurCover(state) {
			let curIndex = state.currentPlayIndex
			return musics[curIndex].cover
		}
	},
	mutations: {
		// 销毁
		destruction() {
			audio.offPlay()
			audio.offPause()
			audio.offStop()
			audio.offEnded()
			audio.offError()
		},
		// 播放
		audioPlay(state) {
			let curIndex = state.currentPlayIndex
			state.audioList[curIndex].playStatus = 1
			if (audio.src != musics[curIndex].src) {
				audio.src = musics[curIndex].src
			}
			audio.play()
		},
		// 跳转
		audioSeek(state, pos) {
			audio.seek(pos)
		},
		// 暂停
		audioPause(state) {
			let curIndex = state.currentPlayIndex
			state.audioList[curIndex].playStatus = -1
			audio.pause()
		},
		// 停止
		audioStop(state) {
			let curIndex = state.currentPlayIndex
			state.audioList[curIndex].playStatus = 0
			audio.stop()
		},
		// 改变播放与暂停标识
		changePlayStatus(state, Boolean) {
			state.playStatus = Boolean
		},
		// 改变播放标识
		changePlayIndex(state, index) {
			state.currentPlayIndex = index
		},
		// 获取总时长
		getDurationTime(state, time) {
			state.durationTime = time
		},
		// 改变当前音频时间(暂停时)
		changeCurrentTime(state, time) {
			state.currentTime = time
		},
		// 获取音频列表
		getAudioList(state, audioList) {
			for (let item of audioList) {
				state.audioList.push({
					id: item.id,
					audioName: item.name,
					singerName: item.singer.name,
					playStatus: 0 // -1 暂停 0 停止 1 播放
				})
			}
		}
	},
	actions: {
		// 初始化
		init({
			state,
			commit,
			dispatch
		}) {
			if (audio) return
			audio = uni.createInnerAudioContext() // 实例化audio对象
			// #ifdef H5
			audio.src = musics[0].src
			// #endif
			commit('getAudioList', musics)
			// 监听
			// #ifdef MP
			audio.onCanplay(() => {
				commit('getDurationTime', audio.duration)
				commit('changeCurrentTime', audio.currentTime)
			})
			// #endif
			// 播放事件
			audio.onPlay(() => {
				commit('changePlayStatus', true)
			})
			// 暂停事件
			audio.onPause(() => {
				commit('changePlayStatus', false)
			})
			// 停止事件
			audio.onStop(() => {
				commit('changePlayStatus', false)
			})
			// 播放结束事件
			audio.onEnded(() => {
				commit('changePlayStatus', false)
				dispatch('PreOrNext', 'next')
			})
			// 播放错误事件
			audio.onError((res) => {
				console.info(res.errMsg)
				console.info(res.errCode)
				commit('changePlayStatus', false)
			})
			// 播放当前时长
			audio.onTimeUpdate(() => {
				commit('getDurationTime', audio.duration)
				commit('changeCurrentTime', audio.currentTime)
			})
		},
		// 播放与暂停
		PlayOrPause({
			state,
			commit
		}) {
			if (!state.playStatus) {
				commit('audioPlay')
			} else {
				commit('audioPause')
			}
		},
		// 切歌
		PreOrNext({
			state,
			commit
		}, type) {
			commit('audioStop')
			let curIndex = state.currentPlayIndex,
				lastIndex = musics.length - 1
			switch (type) {
				case 'pre':
					curIndex === 0 ? commit('changePlayIndex', lastIndex) : commit('changePlayIndex', curIndex - 1)
					break;
				case 'next':
					curIndex === lastIndex ? commit('changePlayIndex', 0) : commit('changePlayIndex', curIndex + 1)
					break;
				default:
					break;
			}
			commit('audioPlay')
		},
		// 滑动进度条
		sliderToPlay({
			state,
			commit
		}, {
			detail: {
				value: position
			}
		}) {
			let time = position
			commit('audioSeek', position)
			// app打包后seek将不触发TimeUpdate所以干脆都走commit('changeCurrentTime', time)
			clearTimeout(timeout)
			timeout = setTimeout(() => commit('changeCurrentTime', time), 200)
		},
		// 列表选择播放
		selectPlay({
			state,
			commit
		}, id) {
			let curIndex = musics.findIndex(item => item.id === id)
			if (state.currentPlayIndex === curIndex) {
				if (state.playStatus) {
					commit('audioPause')
				} else {
					commit('audioPlay')
				}
				return
			} else {
				commit('audioStop')
			}
			commit('changePlayIndex', curIndex)
			commit('audioPlay')
		}
	}
}
